﻿using AutoMapper;
using BackData;
using Domain;
using Infratructure;
using MediatR;
using Server.API.Application.Command.Goods;
using Server.API.DTOs;

namespace Server.API.Application.Handler.Goods
{
    public class OneGetGoodsHandler:IRequestHandler<OneGetGoodsCommand,APIResult<GoodDTO>>
    {
        private readonly IRepository<GoodsModel> goodsway;
        private readonly IMapper mapper;
        private readonly ILogger<OneGetGoodsHandler> log;

        public OneGetGoodsHandler(IRepository<GoodsModel> goodsway, IMapper mapper, ILogger<OneGetGoodsHandler> log)
        {
            this.goodsway = goodsway;
            this.mapper = mapper;
            this.log = log;
        }
        /// <summary>
        /// 获取单个商品数据
        /// </summary>
        /// <param name="request">商品主键</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>商品信息</returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<GoodDTO>> Handle(OneGetGoodsCommand request, CancellationToken cancellationToken)
        {
            APIResult<GoodDTO> res=new APIResult<GoodDTO>();

           var value =await goodsway.GetValue(request.Id);
            if (value == null)
            {
                res.Num = StateCode.Fail;
                res.Message = "商品不存在";    
            }
            else
            {
                res.Data = mapper.Map<GoodDTO>(value);
                res.Num = StateCode.OK;
                res.Message = "获取成功";
            }

            return res;

        }
    }
}
